﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Drawing;
using System.Threading;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS_物流发运单发运计划行数量卡控
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMSWaybillPlanQtyCheckController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public TMSWaybillPlanQtyCheckController(ILogger<TMSWaybillPlanQtyCheckRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            _logger = loggerFactory.CreateLogger<TMSWaybillPlanQtyCheckController>();

        }

        /// <summary>
        ///TMS_物流发运单发运计划行数量卡控
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostlPlanQtyCheck([FromBody] TMSWaybillPlanQtyCheckRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            string waybill_no = "";
            waybill_no = strparm.waybill_no;
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMSWaybillPlanQtyCheckResponseDto re = new TMSWaybillPlanQtyCheckResponseDto();
            string sysid = "";

            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            Dictionary<string, string> plandic = new Dictionary<string, string>(); //计划表插入dic

            if (waybill_no==null)
            {
                sysid = "TMS0153";
            }
            else
            {
                sysid = "TMS0152";
            }


            try
            {
                if (strparm.rows != null)
                {                   
                    foreach (var item in strparm.rows)
                    {
                        dic = new Dictionary<string, string>(); //查询除本发货申请外，总共做了多少数量
                        dic.Add("plan_no", item.plan_no.Trim().ToString());
                        dic.Add("waybill_no", waybill_no);
                        //DataTable dt_check = MethodHelp.GetTableInfo("TMS0128", dic,con);
                        DataTable dt_check = MethodHelp.GetTableInfo(sysid, dic, con);
                        if (dt_check.Rows.Count>0)
                        {
                            double qty_s = DB.ToDouble(dt_check.Rows[0]["ship_qty"].ToString())+ DB.ToDouble(item.qty);
                            double so_qty = DB.ToDouble(dt_check.Rows[0]["materialamount"].ToString());

                            if (qty_s> so_qty)
                            {
                                strMsg = strMsg + "-计划流水<" + item.plan_no.Trim().ToString() + ">，计划流水总数量<"+ so_qty + ">,已发运+本次数量<"+ qty_s + ">,禁止超计划量派车！";
                            }
                        }
                        //找不到代表只有当前单据号的数据
                        //else
                        //{
                        //    strMsg = strMsg + "-找不到计划流水<"+ item.plan_no.Trim().ToString() + ">相关信息，请确认后再试！";
                        //}

                    }                   
                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
